
/*Diego Lima de Freitas RA: 317632
 * Yuri Souza Padua RA: 317420
 * 
 * Classe que trata o comando de atribuição da linguagem
 */
package AST;

public class AssignmentCommand extends Command {

    private VarDec v;
    private Expr expr;

    public AssignmentCommand(VarDec v, Expr expr) {
        this.v = v;
        this.expr = expr;
    }

    @Override
    public void genC(PW pw) {

        if (expr instanceof GetIntegerExpr) {
            //GetIntegerExpr e = (GetIntegerExpr) expr;
            VarDec _auxGetInteget = new VarDec("_"+this.v.getName(), Type.integerType);
            
            pw.add();
            pw.out.println("   int " + _auxGetInteget.getName() + ";");
            pw.out.println("   scanf(" + "\"%d\"" + ", " + "&" + _auxGetInteget.getName() + ");");            
            pw.out.println("   "+this.v.getName() + " = " + _auxGetInteget.getName()+ ";");
            pw.sub();
        }else{
            pw.print(v.getName() + " = ");
            expr.genC(pw, false);
            pw.out.println(";");
        }
        
//        if( expr instanceof GetIntegerExpr){
//            VarDec get = new VarDec("__getIntegerVar", Type.integerType);
//            pw.out.println("   int "+get.getName() + ";");
//            pw.out.println("   scanf("+"\"%d\""+ ", "+"&"+get.getName()+");");
//        }
//        
//        pw.print(v.getName() + " = ");
//        expr.genC(pw, false);
//        pw.out.println(";");
//


    }
}
